package cn.panda.edu.controller;

import cn.panda.edu.model.Course;
import cn.panda.edu.model.Notice;
import cn.panda.edu.model.Student;
import cn.panda.edu.model.Video;
import cn.panda.edu.service.*;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/api/student")
public class StudentLiController {

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private SectionService sectionService ;
	@Autowired
	private PartSectionService partSectionService ;
	@Autowired
	private VideoService videoService;

	@ApiOperation(value = "获取所有公告", notes = "李仕阳")
	@ApiImplicitParam(name = "pageIndex", value = "当前页面", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/listNotice", method = RequestMethod.POST)
	@ResponseBody
	public Object getListNotice(Integer pageIndex) {
		if (pageIndex == null) {
			pageIndex = 1;
		}
		PageInfo<Notice> pageInfo = noticeService.selectNoticeByPage(pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", pageInfo);
	}

	@ApiOperation(value = "获取公告详细内容", notes = "李仕阳")
	@ApiImplicitParam(name = "noticeId", value = "当前公告id", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/getNoticeById", method = RequestMethod.POST)
	@ResponseBody
	public Object getNoticeById(Integer noticeId) {
		return ResultDataUtil.resData(200, "", noticeService.selectByPrimaryKey(noticeId));
	}

	@ApiOperation(value = "获取学生对应课程", notes = "")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageIndex", value = "当前页面", required = true, dataType = "int", paramType = "form"),
	      @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "int", paramType = "form")
	})
	@RequestMapping(value = "/listCourse", method = RequestMethod.POST)
	@ResponseBody
	public Object getListCourse(HttpSession session, Integer pageIndex) {
		Student student = (Student) session.getAttribute(Constant.LOGIN_USER);
		if (pageIndex == null) {
			pageIndex = 1;
		}
		PageInfo<Course> pageInfo = courseService.selectCourseByPage(student.getStudentId(), pageIndex, 10) ;
		return ResultDataUtil.resData(200, "", pageInfo);
	}

	@ApiOperation(value = "获取课程详细章节", notes = "")
	@ApiImplicitParam(name = "id", value = "当前课程的id", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/getCourseById", method = RequestMethod.POST)
	@ResponseBody
	public Object getCourseById(Integer courseId, Integer pageIndex) {

		return ResultDataUtil.resData(200, "", sectionService.selectByCourseId(courseId, pageIndex)) ;
	}
	@ApiOperation(value = "获取章节详细信息", notes = "")
	@ApiImplicitParam(name = "sectionId", value = "当前章节id", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/getPartSectionBySectionId", method = RequestMethod.POST)
	@ResponseBody
	public Object getPartSectionBySectionId(Integer sectionId, Integer pageIndex) {
		return ResultDataUtil.resData(200, "", partSectionService.selectBySectionId(sectionId , pageIndex));
	}
	
	@ApiOperation(value = "点播视频", notes = "")
	@ApiImplicitParam(name = "videoId", value = "当前视频的Id", required = false, dataType = "int", paramType = "form")
	@RequestMapping(value = "/downloadViedo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> downloadFile(Integer videoId) {
		Video video = videoService.selectByPrimaryKey(videoId) ;
		return ResultDataUtil.resData(200, "", video);
	}

}
